package views.swing;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class CuestionarioLayout implements LayoutManager{
	Vector labels=new Vector();
	Vector fields=new Vector();
	Vector options=new Vector();

	int yGap = 5;
	int xGap = 10;

	public void addLayoutComponent(String s, Component c) {
		if (s.equals("label")) {
			labels.addElement(c);
		}else{
			fields.addElement(c);
		}
	}

	public void layoutContainer(Container c) {
		Insets insets = c.getInsets();

		int labelWidth = 0;
		Enumeration labelIter = labels.elements();
		while(labelIter.hasMoreElements()) {
			JComponent comp = (JComponent)labelIter.nextElement();
			labelWidth = Math.max( labelWidth, comp.getPreferredSize().width );
		}

		int yPos = insets.top;

		Enumeration fieldIter = fields.elements();
		labelIter = labels.elements();
		while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
			JComponent label = (JComponent)labelIter.nextElement();
			JComponent field = (JComponent)fieldIter.nextElement();
			int height = Math.max(label.getPreferredSize().height, field.getPreferredSize().height);
			label.setBounds( insets.left, yPos, labelWidth, height ); 
			field.setBounds( insets.left + labelWidth + xGap, yPos, field.getPreferredSize().width, height); 
			yPos += (height + yGap);
		}
	}


	public Dimension minimumLayoutSize(Container c) {
		Insets insets = c.getInsets();
		  
		int wPreguntas=0;
		int altura=insets.top;
		
		Enumeration labelIter = labels.elements();
		Enumeration fieldIter = fields.elements();
	      
		while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
			JComponent label = (JComponent)labelIter.nextElement();
			JComponent field = (JComponent)fieldIter.nextElement();
			
			wPreguntas=Math.max(wPreguntas, label.getPreferredSize().width+field.getPreferredSize().width);
			altura+=Math.max(label.getPreferredSize().height, field.getPreferredSize().height)+yGap;
		}
		return new Dimension(insets.left+wPreguntas+insets.right , altura+insets.bottom);
	}
  
	public Dimension preferredLayoutSize(Container c) {
		return minimumLayoutSize(c);
	}
   
	public void removeLayoutComponent(Component c) {}

}